package com.hly.versioncontroladmin.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.versioncontroladmin.domain.Bo.VcApplicationBo;
import com.hly.versioncontroladmin.domain.VcApplication;
import com.hly.versioncontroladmin.mapper.VcApplicationMapper;
import com.hly.versioncontroladmin.mapper.VcCategoryMapper;
import com.hly.versioncontroladmin.result.PageQuery;
import com.hly.versioncontroladmin.service.VcApplicationService;
import com.hly.versioncontroladmin.service.VcVersionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class VcApplicationServiceImpl extends ServiceImpl<VcApplicationMapper, VcApplication> implements VcApplicationService {

    private final VcCategoryMapper categoryMapper;
    private final VcVersionService versionService;

    @Override
    public Page<VcApplication> listQuery(PageQuery<VcApplication> pageQuery, VcApplicationBo bo) {
        Page<VcApplication> page = buildQueryWrapper(bo).page(pageQuery.builderPage());
        page.getRecords().forEach(n -> {
            n.setCategory(categoryMapper.selectById(n.getCategoryId()));
            n.setVersion(versionService.getNewAppVersion(n.getId(), 1));
        });
        return page;
    }

    public LambdaQueryChainWrapper<VcApplication> buildQueryWrapper(VcApplicationBo bo){
        LambdaQueryChainWrapper<VcApplication> queryChainWrapper = lambdaQuery()
                .eq(bo.getCategoryId() != null, VcApplication::getCategoryId, bo.getCategoryId())
                .like(bo.getName() != null, VcApplication::getName, bo.getName())
                .like(bo.getInfo() != null, VcApplication::getInfo, bo.getInfo());
        return queryChainWrapper;
    }
}
